####Replication for Figures 1 and 2 in "Everyday Illiberalism" (Jakli and Stenberg)

setwd('~/Desktop')

library(ggplot2)
library(dplyr)
library(plyr)
library(interplot)
library(GGally)
library(reshape2)
library(tidyr)


##### read in data with # of candidates per election year, by party ideology ########

party1<-read.csv("partydata.csv")

###aggregate number of candidates by election year
total_year<- party1 %>%
  dplyr::group_by(YEAR) %>%
  dplyr::summarize(total_number = sum(NUMBER))
  
###merge in yearly aggregate (denominator) with the party candidates data
party<-merge(
  x=party1, 
  y=total_year,
  by=c("YEAR"))

###sum Fidesz and KDNP for observations from the 2006 election season onward, since they are in a coalition
fidesz_party<-party %>%
  filter(
    (YEAR < 2006 & PARTY == 'FIDESZ') |
    (YEAR >= 2006 & (PARTY == 'FIDESZ' | PARTY == 'KDNP'))
  ) %>%
  dplyr::group_by(YEAR, total_number) %>%
  dplyr::summarize(
    total_fidesz = sum(NUMBER),
  )
fidesz_party$percent_fidesz<-(fidesz_party$total_fidesz / fidesz_party$total_number)*100
  
fidesz_party$rounded<-round(fidesz_party$percent_fidesz, 1)
 

####FIGURE 1 plot####   

ggplot(data = fidesz_party, aes(x =YEAR, y = percent_fidesz, label=rounded)) + geom_point(size=2.5, color="black") +
  xlab("Election year") +
  ylab("% of Fidesz-coalition among major party candidates") +
  geom_text(hjust = 0, nudge_x = 0.17, color="black", size=3)+
  geom_line(color="black")+
  scale_y_continuous(limits = c(0, 100), breaks=(seq(0, 100, 20)))+
  scale_x_continuous(limits = c(2002, 2014.5), breaks=c(2002, 2006, 2010, 2014))+
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14))+
  theme_classic()

ggsave("fig1_governance.jpg", units="in", width=10, height=8, dpi=600)


####FIGURE 2####  

###group party observations by ideology

party1$ideology[party1$PARTY=='CENTRUM' | party1$PARTY== 'DK' | party1$PARTY=='LMP' | 
                  party1$PARTY=='MSZP' | party1$PARTY=='SZDSZ' | party1$PARTY== 'WP']<-'Left'


party1$ideology[party1$PARTY=='FIDESZ'| party1$PARTY=='FKGP' | party1$PARTY=='KDNP' | 
                  party1$PARTY=='MDF']<-'Right'

party1$ideology[party1$PARTY=='JOBBIK' | party1$PARTY=='MIÉP' ]<-'Radical Right'


###FIGURE 2 plot###

ggplot(party1[order(party1$ideology, decreasing = T),], 
       aes(fill=factor(ideology, levels=c("Radical Right", "Right", "Left")), y=NUMBER, x=YEAR)) + 
  geom_bar(position="fill", stat="identity", colour = "NA")+
  xlab("Election year") +
  ylab("% of major party candidates") +
  scale_y_continuous(labels = scales::percent_format())+
  guides(fill=guide_legend(title="Party Ideology"))+ 
  scale_fill_manual(values=c("#009E73", "#E69F00", "#0072B2"))+
  scale_x_continuous(limits = c(2000, 2016), breaks=c(2002, 2006, 2010, 2014))+
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14))+
  theme_classic()

ggsave("fig2_governance.jpg", units="in", width=10, height=8, dpi=600)
